package jmu.edu.yjc.service.answer;

import jmu.edu.yjc.domain.entity.UnAnsweredQuestion;
import jmu.edu.yjc.service.UnAnsweredQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yjc
 * @date 2023/3/12 16:48
 */
@Service
public class AnswerService {
    @Autowired
    private QuestionClassifier questionClassifier;
    @Autowired
    private QuestionParser questionParser;
    @Autowired
    private AnswerParser answerParser;
    @Resource
    private UnAnsweredQuestionService unAnsweredQuestionService;
    public List<String> getAnswer(String questions , Long userID){
        List<String> answers = new ArrayList<>();
        String[] split = questions.split(",");
        for (String question : split) {
            ClassifierResult classifier = questionClassifier.getQuestionClassifier(question);
            if(classifier.getQuestionTypes().size() == 0 || classifier.getDict().size() == 0){
                String possibleCause = "可能的原因：";
                if(classifier.getQuestionTypes().size() == 0){
                    possibleCause += "没有匹配到合适的问题类型 ";
                }
                if(classifier.getDict().size() == 0){
                    possibleCause += "缺少关键字 ";
                }
                possibleCause += ";" + "questionTypes: " + classifier.getQuestionTypes().toString() + "final_dict: " + classifier.getDict().toString();
                UnAnsweredQuestion unAnswered = UnAnsweredQuestion.builder().content(question)
                        .createBy(userID).createTime(new Date()).possibleCause(possibleCause).build();
                unAnsweredQuestionService.save(unAnswered);
                return new ArrayList<>();
            }
            Map<String, List<String>> searchSql = questionParser.getSearchSql(classifier);
            List<String> answer = answerParser.finalAnswer(searchSql);
            answers.addAll(answer);
        }
        return answers;
    }
}
